package com.jfinalbbs.utils;

import java.io.File;
import java.io.IOException;

import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.StaticLog;
import com.xiaoleilu.hutool.setting.Setting;
import com.xiaoleilu.hutool.util.CharsetUtil;
import com.xiaoleilu.hutool.util.SecureUtil;

public class SendmailUtils {
	private final static Log log = StaticLog.get();
	private static String[] telltowhos;// 邮件接收人列表
	private static EmailUtils eutil;// 初始化

	/**
	 * 发送邮件工具调用实例
	 * 
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		System.out.println(new File("src/main/resources/checkurl.config").getAbsoluteFile());
		telltowhos = null;
		// 初始化tocheck
		doinitemails();// 初始化

		if (null != telltowhos) {
			for (String a : telltowhos) {
				System.out.println(a);
				eutil.sendSimpleEmail(a, "智慧社区自动巡检报告", "智慧社区和大数据数据库同步今日同步已完成!测试", null);
			}
		}
	}

	public static void doinitemails() {
		// TODO Auto-generated method stub
		log.info("开始初始化邮件工具");
		Setting ss = new Setting(new File("src/main/resources/checkurl.config").getAbsoluteFile(), Setting.DEFAULT_CHARSET, false);

		String server = null;
		String email = null;
		String passwd = null;
		server = ss.get("email_server", "sendmail");
		email = ss.get("email_email", "sendmail");
		passwd = ss.get("email_passwd_base64", "sendmail");
		//passwd = SecureUtil.decodeBase64(passwd, CharsetUtil.UTF_8);
		eutil = new EmailUtils();
		eutil.init(server, email, passwd);
		telltowhos = ss.getStrings("tell", "tellto");
		for (String a : telltowhos) {
			log.info("收件人：" + a);
		}
		log.info("邮件初始化完成.");
	}

	// 其他类调用的公共方法
	public static void send(String title, String content) throws IOException {
		if (null == eutil)
			doinitemails();
		// TODO Auto-generated method stub
		if (null != telltowhos) {
			for (String a : telltowhos) {
				eutil.sendSimpleEmail(a, title, content, null);
			}
		} else {
			log.debug("没有邮件通知人.!");
		}
	}
}
